package com.indobrains.uud1945;

import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

import com.indobrains.uud1945.database.DatabaseAdapter;

public class Pasal2Activity extends ExpandableListActivity implements OnChildClickListener,OnGroupClickListener {
	private static final String TAG = "Clauses";
	private static final String CHAPTER_ID = "_id";
	private static final String CHAPTER_TITLE = "title";
	private static final String SEARCH_KEYWORD = "_keyword";
	private ExpandableListView elv;
	private DatabaseAdapter db;
	private Cursor cursor;
	Intent intent; 
	Dialog SearchDialog;
	private EditText keyword;
	private Button btnSearch;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pasal_layout);
		TextView title=(TextView)findViewById(R.id.judul_bab);
		
		Bundle b = getIntent().getExtras();
		if (b!=null){
		int chapter_id=b.getInt(CHAPTER_ID);
		title.setText(b.getString(CHAPTER_TITLE));
		
		elv=getExpandableListView();
		
		
		setClauseAdapter(elv, chapter_id);
		//int count=elv.getAdapter().getCount();
		}
		elv.setOnChildClickListener(this);
		elv.setOnGroupClickListener(this);
		
		//Log.d(TAG,"Clause Count : "+Integer.toString(count));
	}
	public void setClauseAdapter(ExpandableListView view,int chapter_id){
		try {
			db=new DatabaseAdapter(this);
			db.Open();
			cursor=db.getClauses(chapter_id);
			//startManagingCursor(cursor);
			if (cursor.getCount()>0){
			String[] GroupFrom = new String[] {db.CLAUSE_ID, db.CLAUSE_TITLE };
			int[] GroupTo = new int[] {R.id.pasal_id, R.id.judulPasal};
			String[] ChildFrom=new String[]{db.CONTENT_BULLET,db.CONTENT_CONTENT,db.AMANDEMEN_DESC,db.CONTENT_OLDER};
			int[] ChildTo=new int[]{R.id.bullet,R.id.isi_pasal,R.id.amandemen,R.id.older_version};
			//MenuAdapter adapter=new MenuAdapter(getActivity(), R.layout.list_item, cursor, from, to);
			//Pasal2Adapter adapter = new Pasal2Adapter(this,R.layout.pasal_item,cursor,from,to);
			Pasal2Adapter adapter=new Pasal2Adapter(this,cursor,R.layout.group_pasal,GroupFrom,
					GroupTo,R.layout.pasal_child,ChildFrom,ChildTo); 
			//view.setAdapter(adapter);
			setListAdapter(adapter);
			}
		} catch (Exception ex){
			//Log.e(TAG,ex.getMessage());
		}
	}
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		//return super.onChildClick(parent, v, groupPosition, childPosition, id);
		//parent.expandGroup(groupPosition);
		return false;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.search, menu);
		return true;
	}
	public void searchDialog(){
		SearchDialog=new Dialog(this);
		SearchDialog.setTitle("Masukkan kata kunci");
		SearchDialog.setContentView(R.layout.search_dialog);
		keyword=(EditText)SearchDialog.findViewById(R.id.keyword);
		btnSearch=(Button)SearchDialog.findViewById(R.id.btnSearch);
		
		btnSearch.setOnClickListener(search_button_click_listener);
		SearchDialog.show();
	}
	private OnClickListener search_button_click_listener = new OnClickListener() {
	    public void onClick(View v) {
	    	SearchDialog.dismiss();
	    	String s_keyword=keyword.getText().toString();
	    	intent=new Intent(Pasal2Activity.this,SearchActivity.class);
	    	Bundle b=new Bundle();
	    	b.putString(SEARCH_KEYWORD, s_keyword);
	    	intent.putExtras(b);
	    	startActivity(intent);
	    	
	    }
	};
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()){
		case R.id.search:
			 searchDialog();
			break;
		case R.id.chapter:
			intent=new Intent(Pasal2Activity.this,ChapterActivity.class);
			startActivity(intent);
			break;
		}
		return false;
	}
	@Override
	public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
			long id) {
		// TODO Auto-generated method stub
		//parent.expandGroup(groupPosition);
		return false;
	}
}
